package com.ksd.web.controller.tool;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import com.ksd.common.core.domain.AjaxResult;
import com.ksd.common.utils.StringUtils;
import com.ksd.system.domain.SysTvData;
import com.ksd.system.domain.SysTvType;
import com.ksd.web.controller.upload.*;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.File;
import java.util.List;

public class XmlTypeJudge {
    public static AjaxResult judgeType(String xmlPath, String typeData) {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            if (typeData.equals(SysTvType.movieDefaultId)){
                System.out.println("电影类型");
                JAXBContext jaxbContext = JAXBContext.newInstance(MovieResources.class);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                MovieResources resources = (MovieResources) unmarshaller.unmarshal(new File(xmlPath));
                MovieContent content = resources.getContent();
//                System.out.println("电影类型:"+content.getCategory());
                if ("影视中心".equals(content.getCategory())){
                    System.out.println("影视中心======================");
                    return ajaxResult.success(content.getCategory());
                }
            }else if(typeData.equals(SysTvType.songDefaultId)){
                JAXBContext jaxbContext = JAXBContext.newInstance(SongResources.class);
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                SongResources resources = (SongResources) unmarshaller.unmarshal(new File(xmlPath));
                SongContent content = resources.getContent();
//                System.out.println("音乐类型:"+content.getCategory());
                if ("纯音乐享".equals(content.getCategory())){
                    System.out.println("纯音乐享======================");
                    return ajaxResult.success(content.getCategory());
                }
            }else{
                return ajaxResult.error("文件校验失败:不支持其他类型:"+typeData);
            }
            return ajaxResult.error("文件校验失败:选择的内容类型:"+typeData+"与实际上传的文件数据不匹配.xml:"+xmlPath);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("xml文件解析错误:"+xmlPath);
            return AjaxResult.error("文件校验失败:xml文件解析错误:"+xmlPath);
        }
    }


    public static void main(String[] args) {
        String path1 = "C:\\Users\\wuyun\\Desktop\\首页照片\\电影\\bbswxr\\config.xml";
        String path2 = "C:\\Users\\wuyun\\Desktop\\首页照片\\歌曲\\gfcyx\\config.xml.done";
        AjaxResult result = judgeType(path1,SysTvType.songDefaultId);
        System.out.println(result.toString());
        System.out.println(result.isSuccess());
    }
}
